$(function() {
	if (window.errorMsg) {
		$("#alert-warning").append(window.errorMsg).removeClass("hidden").hide().fadeIn("slow");
		$("#alert-warning").fadeOut(5000);
	}
	validationOption = {
		rules : {
			name : {
				minlength : 2,
				maxlength : 20,
				required : true
			},
			deviceKey : {
				required : true
			},
			addr1 : {
				required : true
			}
		},
		messages : {
			name : {
				minlength : "总表名应多于2个字母！",
				maxlength : "总表名应少于20个字母！",
				required : true
			},
			deviceKey : {

				required : "请输入设备标识！"
			},
			addr1 : {
				required : "请输入一级地址！"
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		unhighlight : function(element) {
			$(element).closest('.form-group').removeClass('has-error');
		},
		errorElement : 'span',
		errorClass : 'help-block',
		errorPlacement : function(error, element) {
			if (element.parent('.input-group').length) {
				error.insertAfter(element.parent());
			} else {
				error.insertAfter(element);
			}
		}

	};
	$('#addForm').validate(validationOption);
	$('#editForm').validate(validationOption);
	var table = $('#meter')
			.DataTable(
					{
						"processing" : true,
						"serverSide" : true,
						"dom" : 'tr<"bottom"ip>',
						"ajax" : {
							"url" : window.basePath + "meter/list",
							"type" : "POST",
						},
						"language" : {
							"zeroRecords" : "无数据！",
							"infoEmpty" : "没有找到该条记录！",
							"infoFiltered" : "(共 _MAX_ 条记录)",
							"processing" : "加载中...",
							"lengthMenu" : "_MENU_ Einträge anzeigen",
							"info" : "显示第_START_ 到第 _END_条记录 （共 _TOTAL_ 条记录）",
							"infoPostFix" : "",
							"paginate" : {
								"first" : "第一页",
								"previous" : "上一页",
								"next" : "下一页",
								"last" : "最后一页"
							}
						},
						"columns" : [
								{
									"class" : 'details-control',
									"orderable" : false,
									"data" : null,
									"defaultContent" : ''
								},
								{
									"data" : "name"
								},
								{
									"data" : "deviceKey"
								},
								{
									"data" : "addr1"
								},
								{
									"targets" : -1,
									"class" : "ops",
									"orderable" : false,
									"data" : null,
									"searchable" : false,
									"defaultContent" : "<button type='button' class='btn btn-success btn-sm'>编辑</button>&nbsp;<button type='button' class='btn btn-danger btn-sm'>删除</button>"
								} ],
						"order" : [ [ 1, "asc" ] ]

					});
	$.fn.dataTable.ext.errMode = 'throw';
	$("#searchName").on('keyup', function() {
		table.column(0).search(this.value).draw();
	});
	$("#searchDeviceKey").on('keyup', function() {
		table.column(1).search(this.value).draw();
	});
	$("#searchAddr1").on('keyup', function() {
		table.column(2).search(this.value).draw();
	});

	$('#meter tbody').on('click', 'td.details-control', function() {
		var tr = $(this).parents('tr');
		var row = table.row(tr);

		if (row.child.isShown()) {
			row.child.hide();
			tr.removeClass('shown');
		} else {
			row.child(format(row.data())).show();
			tr.addClass('shown');
		}
	});

	function format(data) {
		return '<table  class="table"><tr><td>累计热能：</td><td><em>' + Math.round(data.allheat * 100) / 100.0
				+ ' kWh</em></td><td>累计流量：</td><td><em>' + Math.round(data.allflow * 100) / 100.0
				+ ' m<SUP>3</SUP</em></td><td>瞬时热量：</td><td><em>' + data.currentHeat + ' kW</em></td><td>瞬时流量：</td><td><em>'
				+ Math.round(data.currentflow * 100) / 100.0 + ' m<SUP>3</SUP>/h</em></td></tr><tr><td>上个分摊周期热量:</td><td><em>'
				+ Math.round(data.lastheat * 100) / 100.0 + ' kW</em></td>' + '<td>进水温度：</td><td><em>'
				+ Math.round(data.inTemperature * 10) / 10.0 + ' ℃</em></td><td>回水温度：</td><td><em>' + Math.round(data.outTemperature * 10) / 10.0
				+ ' ℃</em><td>通讯状态：</td><td><em>' + data.status + '</em></td></tr></table>';
	}

	$('#collapseOne').on('show.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-down").addClass("glyphicon glyphicon-chevron-up");
	});
	$('#collapseOne').on('hide.bs.collapse', function() {
		$("#arrow").removeClass("glyphicon glyphicon-chevron-up").addClass("glyphicon glyphicon-chevron-down");
	});
	$("#meter tbody").on("click", "button", function() {
		var data = table.row($(this).parents('tr')).data();
		if ($(this).hasClass('btn-success')) {
			$("#nameEdit").val(data.name);
			$("#deviceKeyEdit").val(data.deviceKey);
			$("#addr1Edit").val(data.addr1);
			$("#idEdit").val(data.id);
			$('#editMeterModal').modal('show');

		} else if ($(this).hasClass('btn-danger')) {
			$("#idDelete").val(data.id);
			$('#deleteMeterModal').modal('show');
		}

	});
});